package tt.dz.biz.dao.user;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.SystemInfoVO;

@Repository("systemInfoDAO")
public class SystemInfoDAO
  extends BaseDAO
{
  private static final String LIST_ALL_VALID_SYSTEMINFO = "select  a.sys_code,a.sys_name,a.sys_desc,a.start_date,a.version,a.owner,a.service_time,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_system_info as a where a.is_valid = 1";
  private static final String LIST_ALL_SYSTEMINFO = "select  a.sys_code,a.sys_name,a.sys_desc,a.start_date,a.version,a.owner,a.service_time,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_system_info as a ";
  private static final String INSERT_SYSTEMINFO = "insert into pub_system_info(sys_code,sys_name,sys_desc,start_date,version,owner,service_time,created_by,created_time,updated_by,updated_time,is_valid)  values(:sysCode,:sysName,:sysDesc,:startDate,:version,:owner,:serviceTime,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid )";
  private static final String UPDATE_SYSTEMINFO = "update  pub_system_info set sys_name = :sysName,sys_desc = :sysDesc,start_date = :startDate,version = :version,owner = :owner,service_time = :serviceTime,created_by = :createdBy,created_time = :createdTime,updated_by = :updatedBy,updated_time = :updatedTime,is_valid = :isValid where sys_code = :sysCode ";
  
  public SystemInfoVO querySystemInfoById(String sysCode)
  {
    String sql = "select  a.sys_code,a.sys_name,a.sys_desc,a.start_date,a.version,a.owner,a.service_time,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_system_info as a  where a.sys_code=:sysCode";
    SystemInfoVO sysInfo = new SystemInfoVO();
    sysInfo.setSysCode(sysCode);
    try
    {
      return (SystemInfoVO)queryForObject(sql, sysInfo, SystemInfoVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (IncorrectResultSizeDataAccessException e)
    {
      return null;
    }
    catch (DataAccessException e)
    {
      LogUtil.error(e.getMessage(), e);
    }
    return null;
  }
  
  public List<SystemInfoVO> listAllSystemInfo()
  {
    return queryForList("select  a.sys_code,a.sys_name,a.sys_desc,a.start_date,a.version,a.owner,a.service_time,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_system_info as a ", null, SystemInfoVO.class);
  }
  
  public List<SystemInfoVO> listAllValidSystemInfo()
  {
    return queryForList("select  a.sys_code,a.sys_name,a.sys_desc,a.start_date,a.version,a.owner,a.service_time,a.created_by,a.created_time,a.updated_by,a.updated_time,a.is_valid  from pub_system_info as a where a.is_valid = 1", null, SystemInfoVO.class);
  }
  
  public int insertSystemInfo(SystemInfoVO systemInfoVO)
  {
    return save("insert into pub_system_info(sys_code,sys_name,sys_desc,start_date,version,owner,service_time,created_by,created_time,updated_by,updated_time,is_valid)  values(:sysCode,:sysName,:sysDesc,:startDate,:version,:owner,:serviceTime,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid )", systemInfoVO);
  }
  
  public int updateSystemInfo(SystemInfoVO systemInfoVO)
  {
    return save("update  pub_system_info set sys_name = :sysName,sys_desc = :sysDesc,start_date = :startDate,version = :version,owner = :owner,service_time = :serviceTime,created_by = :createdBy,created_time = :createdTime,updated_by = :updatedBy,updated_time = :updatedTime,is_valid = :isValid where sys_code = :sysCode ", systemInfoVO);
  }
}
